package cn.bm.web.controllers.user;

import cn.bm.web.services.UserServices;
import spark.Route;

import static spark.Spark.*;

// req 封装 servlet
public class UserController {
    static Route login = (req,res)->{
        // 设置session
        Boolean loginSuccessful = UserServices.login("lys","");
        System.out.println("logined:"+loginSuccessful);
        if(!loginSuccessful){
            res.redirect("/login.html");

        }else {
            req.session().attribute("username","lys");;

        }

        return "login";
    };

    static Route register = (req,res)->{

        return "register";
    };

    static Route test =(req,res)->{
        System.out.println(req.ip());
        req.session().attribute("user","none");

        return "test";

    };



    public static void InitRoutes() {

        /**
         * 开放访问
         */
        System.out.println("init user controller");
        get("/user/login",login);
        post("/user/register",register);
        /**
         * 授权后访问
         */
        get("/app/test",test);
    }
}
